﻿using System.Collections.Generic;
using TheOutfield.UmbExt.UniversalMediaPicker;

namespace Skybrud.UmbExt.UniversalMediaPicker.Providers.Issuu
{
    public static class IssuuExtensions
    {
        public static MediaItem ToMediaItem(this IssuuService.IssuuDoc doc)
        {
            var mi = new MediaItem
                       {
                           Id = doc.DocumentId,
                           Title = doc.Title,
                           Icon = "mediaIssuu.gif",
                           HasChildren = false,
                           PreviewImageUrl = "http://image.issuu.com/" + doc.DocumentId + "/jpg/page_1_thumb_small.jpg",
                           Selectable = true
                       };

            mi.MetaData.Add("Description", doc.Description);
            mi.MetaData.Add("Original Document Name", doc.OrgDocName);
            mi.MetaData.Add("Original Document Type", doc.OrgDocType);
            mi.MetaData.Add("Page Count", doc.PageCount);
            mi.MetaData.Add("Tags", string.Join(", ", doc.Tags.ToArray()));
            mi.MetaData.Add("State", doc.State);
            mi.MetaData.Add("Folders", string.Join(", ", doc.Folders.ToArray()));
            mi.MetaData.Add("View Count", doc.ViewCount);
            return mi;
        }

        public static MediaItem ToMediaItem(this IssuuService.IssuuFolder fldr)
        {
            return new MediaItem
                         {
                             Id = fldr.FolderId,
                             Title = fldr.Name,
                             Icon = UmbracoIcons.FolderOpen,
                             HasChildren = fldr.Items > 0,
                             PreviewImageUrl = "",
                             Selectable = false
                         };
        }

        public static string ListToString(this List<string> list)
        {
            var outputBuilder = new System.Text.StringBuilder();

            foreach (var item in list)
            {
                outputBuilder.AppendLine(item + ", ");
            }

            return outputBuilder.ToString().Substring(0, (outputBuilder.ToString().Length - 4));
        }
    }
}